<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 促销模型
 * @author khj
 */

class MerchantPromotionModel extends MY_Model{
    public $tbUser = 'merchant';
    public $tbCashCoupon = 'merchant_cash_coupon';//代金劵
    public $tbFullCut	= 'merchant_full_cut';//满减劵
    // 支付方式
    public $PAY_TYPE = array(
        '0'=>'暂无支付',
        '1'=>"余额",
        '2'=>"微信",
        '3'=>"支付宝",
    );

    
    /**
     * 通过规则获取满足满减劵条件的最大者
     * @param string $rule 规则 json
     * @param unknown $amount
     * @author zyl
     */
    public function getSatisfyFullCut($rule,$amount){
    	$rule = is_array($rule) ? $rule : json_decode($rule,true);
    	
    	if ( ! $rule){
    		return array('money'=>0,'max_money'=>0);
    	}
    	
    	$money = array('money'=>0,'max_money'=>0);
    	foreach ($rule as $k=>$v){
    		if ($amount >= $v['max_money']){
    			if ($money['money'] < $v['money']){
    				$money = $v;
    			}
    		}
    	}
    	
    	return $money;
    }
    


    /**
     * 支付类型
     * @param null $key
     * @return array|mixed|string
     */
    public function payType($key = null){

        if($key === null){
            return $this->PAY_TYPE;
        }

        return isset($this->PAY_TYPE[$key]) ? $this->PAY_TYPE[$key] : '';
    }
    ////////////////////////////////////////////////////////////////
    //+-----------------------------------------------------------
    //|	代金劵
    //+-------------------------------------------------------
    
    /**
     * 获取代金劵列表
     * @param unknown $merchantId
     * @author zyl
     */
    public function getCouponList($merchantId){
    	if ( ! $merchantId){
    		return array();
    	}
    	
    	$this->db
    	->select('money, valid_hour, receive, condition, addtime')
    	->where('merchant_id',$merchantId);
    	$tA = $this->getAll($this->tbCashCoupon);
    	return $tA;
    }
    
    /**
     * 获取代金劵列表
     * @param array $merchantIds 批量获取
     * @author zyl
     */
    public function getCouponListByMerchantIds(Array $merchantIds){
    	if ( ! $merchantIds){
    		return array();
    	}
    	
    	$this->db
    	->select('merchant_id, money, valid_hour, receive, condition, addtime')
    	->where_in('merchant_id',$merchantIds)
    	->order_by('addtime desc');//时间降序
    	$tA = $this->getAll($this->tbCashCoupon);
    	
    	$tData = array();
    	foreach ($tA as $k=>$v){
    		$tData[$v['merchant_id']] = $v;
    	}
    	
    	return $tData;
    }

    /**
     * 保存商户代金券
     *
     * @param	array	$data	保存数据
     * @param	integer $id		用户id
     * @return	boolean
     * @author khj
     */
    
    public function saveCash($data, $id = 0){
    	
    	if( ! $data) return false;
    	if($id){
    		$this->db->where(array('merchant_id' => $id));
    		if($this->update($this->tbCashCoupon, $data)){
    			return true;
    		}
    	}
    	else{
    		if($id = $this->insert($this->tbCashCoupon, $data)){
    			return $id;
    		}
    	}
    }
    
    /**
     * 获取代金券信息
     * @param $merchantId  商户id
     * @return array
     */
    public function getCashInfo($merchantId = 0){
    	
    	if(!$merchantId)return array();
    	
    	if($merchantId){
    		$this->db->where('c.merchant_id', $merchantId);
    	}
    	
    	$this->db->select('c.merchant_id,c.money,c.valid_hour,c.receive,c.condition,u.able_cash_coupon')
                 ->join($this->tbUser . ' u', 'u.id = c.merchant_id');
    	
    	$tA = $this->getOne($this->tbCashCoupon. ' c');
    	
    	return $tA;
    }
    
    ////////////////////////////////////////////////////////////////
    //+-----------------------------------------------------------
    //|	满减劵
    //+-------------------------------------------------------

    /**
     * 保存商户满减规则
     *
     * @param	array	$data	保存数据
     * @param	integer $id		用户id
     * @return	boolean
     * @author khj
     */

    public function saveFull($data, $id = 0){

        if( ! $data) return false;
        if($id){
            $this->db->where(array('merchant_id' => $id));
            if($this->update($this->tbFullCut, $data)){
                return true;
            }
        }
        else{
            if($id = $this->insert($this->tbFullCut, $data)){
                return $id;
            }
        }
    }

    /**
     * 获取满减信息
     * @param $merchantId  商户id
     * @return array
     */
    public function getFullInfo($merchantId = 0){

        if(!$merchantId)return array();

        if($merchantId){
            $this->db->where('merchant_id', $merchantId);
        }

        $this->db->select('merchant_id,start_time,end_time,rule');

        $tA = $this->getOne($this->tbFullCut);
        if($tA){
            $tA['start_time'] = dateFormat($tA['start_time']);
            $tA['end_time'] = dateFormat($tA['end_time']);
            $tA['rule'] = $tA['rule'] ? json_decode($tA['rule'],true):array();
        }
        return $tA;
    }

    /**
     * 获取代金劵列表
     * @param unknown $merchantId
     * @author zyl
     */
    public function getFullCutList($merchantId){
    	if ( ! $merchantId){
    		return array();
    	}
    	
    	$this->db
    	->select('merchant_id, start_time, end_time, rule, addtime')
    	->where('merchant_id',$merchantId);
    	$tA = $this->getAll($this->tbFullCut);
    	return $tA;
    }
    
    /**
     * 获取代金劵列表
     * @param array $merchantIds 批量获取
     * @author zyl
     */
    public function getFullCutListByMerchantIds(Array $merchantIds){
    	if ( ! $merchantIds){
    		return array();
    	}
    	
    	$this->db
    	->select('merchant_id, start_time, end_time, rule, addtime')
    	->where_in('merchant_id',$merchantIds)
    	->order_by('addtime desc');//时间降序
    	$tA = $this->getAll($this->tbFullCut);
    	
    	$tData = array();
    	foreach ($tA as $k=>$v){
    		$tData[$v['merchant_id']] = $v;
    	}
    	
    	foreach ($merchantIds as $k=>$v){
    		if ( ! array_key_exists($v, $tData)){
    			$tData[$v] = 0;
    		}
    	}
    	return $tData;
    }


	////////////////////////////////////////////////////////////////
	//+-----------------------------------------------------------
	//|	后台
	//+-------------------------------------------------------


    /**
     * 后台获取代金券投放记录
     */
    public function adminPcGetCashCouponList(){
        $merchantId = (int)get('merchant_id');
        $money = trim(get('money'));
        $validHour = trim(get('valid_hour'));
        $sendDateFrom = trim(get('cc_send_date_from'));
        $sendDateTo = trim(get('cc_send_date_to'));
        $iSortCol_0 = (int)get('iSortCol_0');//针对某一列进行排序
        $sSortDir_0 = trim(get('sSortDir_0'));//排序方式，值为"asc"或者"desc"

        if ($merchantId){
            $this->db->where(array('merchant_id' => $merchantId));
        }
        if ($money){
            $this->db->like(array('money' => $money));
        }
        if ($validHour){
            $this->db->like(array('valid_hour' => $validHour));
        }
        if (strlen($sendDateFrom) && strlen($sendDateTo)){
            $this->db->where(array('addtime >= ' => strtotime($sendDateFrom), 'addtime <= ' => strtotime($sendDateTo)));
        }
        if ($iSortCol_0 == 0){
            $this->db->order_by('money', $sSortDir_0);
        }else if($iSortCol_0 == 1){
            $this->db->order_by('valid_hour', $sSortDir_0);
        }else if($iSortCol_0 == 2){
            $this->db->order_by('receive', $sSortDir_0);
        }else if($iSortCol_0 == 3){
            $this->db->order_by('condition', $sSortDir_0);
        }else if($iSortCol_0 == 4){
            $this->db->order_by('addtime', $sSortDir_0);
        }

        $this->db->select('merchant_id,money,valid_hour,receive,condition,addtime')
            ->order_by('addtime desc');
        return $this->pagination($this->tbCashCoupon);
    }


}